package com.kdtech.analyse.blog;
import com.kdtech.crawler.CrawlHTML;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.analyse.AnalyseNews;

public class _163BlogAnalyse implements AnalyseNews{

	//http://caodianwu.blog.163.com/blog/static/21106015920129311240171/?formlife
	//http://blog.163.com/jinsiwang@126/blog/static/13902101120121137525813/?touping
	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://.*blog.163.com/.*blog/static/[0-9]*[/]{0,1}[?a-z]*"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String url = urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		String html = urlMeta.getHtml();
		if (html== null) {
		}
		NewsMeta blog = new NewsMeta();
		blog.setUrl(url);
		String title = null;
		String content = null;
		Long date = null;
		Integer clickNum=0;
		Integer commentNum=0;
		Integer deliverNum=0;
		String author=null;
		Document doc = Jsoup.parse(html);
		doc.select(".snl,div.right").remove();
		title  = doc.select("h3.title span.tcnt").text().trim().replace("　", "");
		date = DateUtils.matchDate(doc.select("span.blogsep").text());
		content = HtmlCleaner.getContentHtml(url,  doc.select("div.mcnt div.nbw-bitm div.bct"));
		author = doc.select("meta[name=author]").attr("content");
		String[] splits  = author.split(",");
		if(splits.length==2){
			author = splits[1];
		}else{
			if(author.endsWith(",")){
				author = author.substring(0,author.length()-1);
			}
		}
		if(StringUtils.isBlank(title)){
		}
		clickNum = NumberUtils.matchNumber(doc.select(":containsOwn(阅读()").text());
		commentNum = NumberUtils.matchNumber(doc.select(":containsOwn(评论()").text());
//		deliverNum = NumberUtils.matchNumber(doc.select(":containsOwn(转载 ()").text());

		blog.setTitle(title);
		blog.setContent(content);
		blog.setDate(date);
		blog.setAuthor(author);
		blog.setClickNum(clickNum);
		blog.setCommentNum(commentNum);
		//blog.setDeliverNum(deliverNum);
		blog.setUpdateUrl(url);
		return blog;
	}

	/*
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			String updateUrl = meta.getUpdateUrl();
			if(updateUrl!=null){
				UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
				String html = responseToURL.getHtml();
				Document doc  = Jsoup.parse(html);
				String textClick  = doc.select("html body#blog-163-com.nb-body div#blog-163-com-main.nb-wrap div.nb-are div.wkg div#blog-163-com-container.c div#-3.nb-mdl div.nb-mc div.c div.nbw-ryt div.left div.lcnt div.mcnt div.nbw-bitm div.nbw-act div div.pright span.fc07:eq(0)").text();
				String textComment = doc.select("html body#blog-163-com.nb-body div#blog-163-com-main.nb-wrap div.nb-are div.wkg div#blog-163-com-container.c div#-3.nb-mdl div.nb-mc div.c div.nbw-ryt div.left div.lcnt div.mcnt div.nbw-bitm div.nbw-act div div.pright span.fc07:eq(2)").text();
				String textDeliver = doc.select("html body#blog-163-com.nb-body div#blog-163-com-main.nb-wrap div.nb-are div.wkg div#blog-163-com-container.c div#-3.nb-mdl div.nb-mc div.c div.nbw-ryt div.left div.lcnt div.mcnt div.nbw-bitm div.nbw-act div div.pright span.m2a").text();
				if(StringUtils.isNotBlank(textClick)
				   	)
				try {
					textClick=textClick.replace("阅读(", "").replace(")", "");
					meta.setClickNum(Integer.parseInt(textClick));
					textComment=textComment.replace("评论(", "").replace(")", "");
					meta.setCommentNum(Integer.parseInt(textComment));
					textDeliver=textDeliver.replace("转载 (", "").replace(") 举报", "");
					meta.setDeliverNum(Integer.parseInt(textDeliver));
					return meta;
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
				}
			}
		}
		return null;
	}





	public static void main(String[] args) {
		String url = "http://duxingheima.blog.163.com/blog/static/25809152201441011172376";
		_163BlogAnalyse test = new _163BlogAnalyse();
		if(test.isDetailPage(url)){
			UrlMeta responseToURL = CrawlHTML.responseToURL(url);
			NewsMeta parserHtml = test.parserHtml(responseToURL);
			System.out.println(parserHtml);
//			System.out.println(test.Update(parserHtml));
		}else{
			System.out.println("不符合规则");
		}
	}

	*/
}
